﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OracleClient;
using System.Data;
using System.Windows.Forms;
using EntityClassGenerator.UserSetting;

namespace EntityClassGenerator.DataAccess
{
    /// <summary>
    /// 本类提供了访问Oracle数据库的相关的方法.
    /// </summary>
    class AccessOracle
    {
        //声明数据库连接.
		private static OracleConnection _connection ; 

		/// <summary>
		/// 默认构造方法.
		/// </summary>
        public AccessOracle()
		{			
		}

		/// <summary>
		/// 本方法的作用是打开数据连接.	
		/// </summary>
        /// <param name="conStr">数据库连接字符串</param>
		public static void OpenConnection(string conStr)
		{
            try
            {
                AccessOracle._connection = new OracleConnection(conStr);
                AccessOracle._connection.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
		}

        /// <summary>
        /// 本方法的作用是打开数据连接.	
        /// </summary>
        public static void OpenConnection()
        {
            if (AccessOracle._connection == null)
            {
                MessageBox.Show("尚未得到数据库连接对象,\n请先调用方法void OpenConnection(string conStr)!");
                return;
            }

            try
            {               
                AccessOracle._connection.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 得到Oracle数据库的联接字符串.
        /// </summary>
        /// <param name="server">服务器名</param>        
        /// <param name="uid">用户名</param>
        /// <param name="pwd">密码</param>
        /// <returns>联接字符串</returns>
        public static string GetConnectionStr(string server,string uid,string pwd)
        {
            return "data source="+server+";user id="+uid+";password="+pwd;  
        }

        /// <summary>
        /// 本方法用于关闭数据库连接.
        /// </summary>
        public static void CloseConnection()
        {
            try
            {
                if (AccessOracle._connection.State == ConnectionState.Open)
                {
                    AccessOracle._connection.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 本方法用于执行返回一个表的相关数据的查询.
        /// </summary>
        /// <param name="sql">SQL命令</param>
        /// <returns>数据表对象</returns>
        public static DataTable GetDataTable(String sql)
        {
            //实例化一个表对象.
            DataTable dataTable = new DataTable();
            try
            {                
                new OracleDataAdapter(sql, AccessOracle._connection).Fill(dataTable);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }            
            //返回表对象.
            return dataTable;
        }

        /// <summary>
        /// 得到所有的表名和视图名.
        /// </summary>        
        /// <returns>表对象</returns>
        public static DataTable GetAllTableAndViewName()
        { 
            try
            {
                return AccessOracle.GetDataTable("select TNAME,TABTYPE from tab where TABTYPE='TABLE' or TABTYPE='VIEW' order by TABTYPE");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);  
            }
            return null;            
        }

        /// <summary>
        /// 得到表或视图的结构.
        /// </summary>        
        /// <param name="tableName">表或视图名</param>
        /// <returns>表对象</returns>
        public static DataTable GetTableOrViewStructure(string tableOrViewName)
        {
            try
            {
                return AccessOracle.GetDataTable("SELECT COLUMN_NAME,DATA_TYPE,DATA_LENGTH FROM USER_TAB_COLUMNS WHERE TABLE_NAME='"+tableOrViewName+"'");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return null;  
        }

        /// <summary>
        /// 将表或视图的结构转换为字段信息.
        /// </summary>
        /// <param name="stru">表或视图的结构信息</param>
        /// <returns>字段信息</returns>
        public static DataTable ConvertTableOrViewStructure(DataTable stru)
        {
            DataTable fields = ToolMethods.GetFieldsTable();
            string fieName = null;
            string fieType = null;
            for (int i = 0; i < stru.Rows.Count; i++)
            {
                fieName =stru.Rows[i][0].ToString();
                fieType = UserSettings.GetMapping(UserSetting.DbType.Oracle, stru.Rows[i][1].ToString());
                fields.Rows.Add(new object[] { fieName, fieType, "" });
            }
            return fields;
        }
    }
}
